package com.wzc.hc.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzc.hc.entity.po.Owner;
import com.wzc.hc.entity.resp.Result;
import com.wzc.hc.mapper.BuildingMapper;
import com.wzc.hc.mapper.OwnerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping("/owner")
@Slf4j
public class OwnerController {

    // 注入OwnerMapper 业主信息
    @Autowired
    private OwnerMapper ownerMapper;

    // 注入BuildingMapper 楼信息
    @Autowired
    private BuildingMapper buildingMapper;

    // 分页查询获取所有业主信息
    @GetMapping("/getowner")
    public Result<IPage<Owner>> getAllOwners(
            @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize", defaultValue = "5") int pageSize) {
        try {
            Page<Owner> page = new Page<>(pageNum, pageSize);
            IPage<Owner> pageResult = ownerMapper.selectPage(page, null);
            return Result.ok(pageResult);
        } catch (Exception e) {
            return Result.fail();
        }
    }

    // 根据id查询所有业主信息
    @GetMapping("/getownerbyid")
    public Result<Owner> getOwnerById(@RequestParam("id") Long id) {
        try {
            Owner owner = ownerMapper.selectById(id);
            if (owner != null) {
                return Result.ok(owner);
            } else {
                return Result.failed("业主信息未找到");
            }
        } catch (Exception e) {
            return Result.fail();
        }
    }

    // 根据姓名查询所有业主信息
    @GetMapping("/getownerbyname")
    public Result<List<Owner>> getOwnersByName(@RequestParam("name") String name) {
        try {
            QueryWrapper<Owner> queryWrapper = new QueryWrapper<>();
            queryWrapper.like("owner_name", name);
            List<Owner> owners = ownerMapper.selectList(queryWrapper);
            return Result.ok(owners);
        } catch (Exception e) {
            return Result.fail();
        }
    }

    // 根据楼层，房屋编号查询所有业主信息








    // 根据手机号查询所有业主信息
    @GetMapping("/getownerbyphone")
    public Result<List<Owner>> getOwnersByPhone(@RequestParam("phone") String phone) {
        try {
            QueryWrapper<Owner> queryWrapper = new QueryWrapper<>();
            queryWrapper.like("owner_contact", phone);
            List<Owner> owners = ownerMapper.selectList(queryWrapper);
            return Result.ok(owners);
        } catch (Exception e) {
            return Result.fail();
        }
    }

    // 根据身份证查询所有业主信息
    @GetMapping("/getownerbyidcard")
    public Result<List<Owner>> getOwnersByIdCard(@RequestParam("idCard") String idCard) {

        try {
            QueryWrapper<Owner> queryWrapper = new QueryWrapper<>();
            queryWrapper.like("owner_identity", idCard);
            List<Owner> owners = ownerMapper.selectList(queryWrapper);
            return Result.ok(owners);
        } catch (Exception e) {
            return Result.fail();
        }
    }

    // 添加业主信息
    @PostMapping("/addowner")
    public Result<Integer> addOwner(@RequestBody Owner owner) {
        try {
            // Add permission check here
            int result = ownerMapper.insert(owner);
            if (result > 0) {
                return Result.ok(result);
            } else {
                return Result.failed("添加业主信息失败");
            }
        } catch (Exception e) {
            return Result.fail();
        }
    }

    // 更新业主信息
    @PutMapping("/updateowner")
    public Result<Integer> updateOwner(@RequestBody Owner owner) {
        try {
            // Add permission check here
            int result = ownerMapper.updateById(owner);
            if (result > 0) {
                return Result.ok(result);
            } else {
                return Result.failed("更新业主信息失败");
            }
        } catch (Exception e) {
            return Result.fail();
        }
    }

    // 删除业主信息
    @DeleteMapping("/deleteowner")
    public Result<Integer> deleteOwner(@RequestParam("id") Long id) {
        try {
            // Add permission check here
            int result = ownerMapper.deleteById(id);
            if (result > 0) {
                return Result.ok(result);
            } else {
                return Result.failed("删除业主信息失败");
            }
        } catch (Exception e) {
            return Result.fail();
        }
    }
}
